use crate::{dto, mq};
use std::sync::Arc;

pub struct EventHandler {
    mq_client_set: Arc<mq::client::ClientSet>,
}

impl EventHandler {
    pub fn new(mq_client_set: Arc<mq::client::ClientSet>) -> Self {
        Self { mq_client_set }
    }
    pub fn spawn_handle_setup_event(
        &self,
        node_id: &str,
        payload: dto::event::endpoint::SetupEventPayload,
    ) {
        let mq_client_set = self.mq_client_set.clone();
        let node_id = node_id.to_string();
        tokio::spawn(async move {
            if let Err(e) = mq_client_set
                .node
                .endpoint
                .publish_setup_event(node_id.as_str(), payload)
                .await
            {
                log::error!("{:?}", e);
            }
        });
    }

    pub fn spawn_handle_delete_event(
        &self,
        node_id: &str,
        payload: dto::event::endpoint::DeleteEventPayload,
    ) {
        let mq_client_set = self.mq_client_set.clone();
        let node_id = node_id.to_string();
        tokio::spawn(async move {
            if let Err(e) = mq_client_set
                .node
                .endpoint
                .publish_delete_event(node_id.as_str(), payload)
                .await
            {
                log::error!("{:?}", e);
            }
        });
    }
}
